<template>
  <el-dialog title="选择学生" :visible.sync="visible" width="90%" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="120px">
      <el-form-item label="姓名" prop="stuName">
        <el-input v-model="queryParams.stuName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="stuSex">
        <el-select v-model="queryParams.stuSex" placeholder="请选择性别" clearable>
          <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="学号" prop="stuCode">
        <el-input v-model="queryParams.stuCode" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="登录手机号" prop="tel">
        <el-input v-model="queryParams.tel" placeholder="请输入登录手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="生日" prop="birthDay">
        <el-date-picker clearable v-model="queryParams.birthDay" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择生日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="校区" prop="schoolName">
        <el-input v-model="queryParams.schoolName" readonly="readonly" @click.native="openSelectSchool(1)"
          placeholder="请输入校区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="省、市、区" prop="queryProvinceAndCityAndDistrict">
        <el-cascader placeholder="请选择省、市、区" v-model="queryParams.queryProvinceAndCityAndDistrict" :options="options"
          @keyup.enter.native="handleQuery" @lazy-load="selectLoadOptions"
          :props="{ lazy: true,checkStrictly:true,multiple: true, lazyLoad: selectLoadOptions,value:'regionId',label:'regionName' }"
          clearable></el-cascader>
      </el-form-item>

      <el-form-item label="上线" prop="state">
        <el-select v-model="queryParams.state" clearable>
          <el-option v-for="dict in dict.type.common_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="studentList" @selection-change="handleSelectionChange"
        height="260px">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="姓名" fixed align="center" prop="stuName" />
        <el-table-column label="头像" align="center" prop="avatarURL">
          <template slot-scope="scope">
            <el-avatar :size="50" :src="scope.row.avatarURL"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column label="性别" align="center" prop="stuSex">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.stuSex" />
          </template>
        </el-table-column>
        <el-table-column label="学号" align="center" prop="stuCode" />
        <el-table-column label="登录手机号" align="center" prop="tel" />
        <el-table-column label="校区" align="center" prop="schoolName" />
        <el-table-column label="省、市、区" align="center" prop="provinceAndCityAndDistrict" />
        <el-table-column label="上线" align="center" prop="state">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.common_state" :value="scope.row.state" />
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectStudent" :loading="submitLoading">
        {{ submitLoading ? '提交中...' : '确定' }}</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    listStudent,
    manStudent
  } from "@/api/bus_web/school/student";

  import {
    mapState
  } from 'vuex';
  import store from "@/store";
  import {
    loadOptions
  } from "@/api/system/sys_region";
  import SelectSchool from "../schoolinfo/select.vue"
  export default {
    dicts: ['sys_user_sex', 'common_state'],
    props: {
      id: {
        type: [Number, String]
      }
    },
    components: {
      SelectSchool
    },
    data() {
      return {
        // 是否显示校区选择
        selectFlag: false,
        submitLoading: false,
        // 遮罩层
        visible: false,
        // 选中数组值
        stuIdList: [],
        options: [],
        // 总条数
        total: 0,
        // 未分配学生数据
        studentList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          stuName: null,
          avatar: null,
          stuSex: null,
          stuCode: null,
          tel: null,
          birthDay: null,
          schoolId: null,
          schoolName: null,
          schoolIdArray: [],
          province: null,
          city: null,
          district: null,
          address: null,
          state: null,
          gmtCreate: null,
          gmtModify: null,
          queryProvinceAndCityAndDistrict: null
        }
      };
    },
    computed: {
      ...mapState({
        school: state => state.selectSchool.school,
        schoolSelectModel: state => state.selectSchool.schoolSelectModel,
      })
    },
    methods: {
      /**省 市 区*/
      selectLoadOptions(node, resolve) {
        var query = {
          regionLevel: node.level + 1,
          parentId: node.value || null
        }
        loadOptions(query).then(response => {
          resolve(response.data);
        });
      },
      // 显示弹框
      show() {
        console.log(this.id)
        this.queryParams.excludeClassId = this.id;
        this.getList();
        this.visible = true;
      },
      clickRow(row) {
        this.$refs.table.toggleRowSelection(row);
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.stuIdList = selection.map(item => item.id);
      },
      // 查询表数据
      getList() {
        this.loading = true;
        listStudent(this.queryParams).then(response => {
          this.studentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 选择授权用户操作 */
      handleSelectStudent() {
        this.submitLoading = true
        const id = this.queryParams.excludeClassId;
        if (this.stuIdList.length < 1) {
          this.$modal.msgError("请选择要分配的学生");
          this.submitLoading = false
          return;
        }
        manStudent({
          id: this.queryParams.excludeClassId,
          op: 1,
          stuIdList: this.stuIdList
        }).then(res => {
          this.$modal.msgSuccess(res.message);
          this.visible = false;
          this.$emit("ok");
        }).finally(() => {
          this.submitLoading = false
        })
      },
      //选择校区
      openSelectSchool(selectVal) {
        this.selectFlag = true
        // selectVal =1 查询模式 =2 创建、修改模式
        this.$store.dispatch('selectSchool/cleanSchool')
        this.$store.dispatch('selectSchool/setSchoolSelectModel', selectVal)
      },
      closeSelectSchool() {
        this.selectFlag = false
      },
      //确定校区
      selectSchoolData() {
        this.closeSelectSchool()
        if (this.schoolSelectModel == 2) {
          this.form.schoolName = this.school[0].schoolName
          this.form.schoolId = this.school[0].id
          this.$refs.form.clearValidate(['schoolName']);
        } else {
          this.queryParams.schoolName = this.school.map(item => item.schoolName).join(',')
          this.queryParams.schoolIdArray = this.school.map(item => item.id)
        }
        this.$store.dispatch('selectSchool/cleanSchool')
      }
    }
  };
</script>
